<template>
  <div class='container'>
    <el-dialog :title="cTitle" :visible.sync="tablevisible" :before-close="gb">
      <el-form ref="formData" :model="formInline" :rules="rules">
        <el-form-item label="所选学科" prop="subjectID" v-if="!this.$route.query.id">
          <el-select v-model="formInline.subjectID" placeholder="请选择">
            <el-option v-for="item in itemlist" :key="item.id" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="所选学科">
           <el-input v-model="formInline.subjectID"></el-input>
        </el-form-item> -->
       <el-form-item label="目录名称" prop="tagName">
          <el-input v-model="formInline.tagName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="gb">取 消</el-button>
        <el-button type="primary" @click="save()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/tags.js'
import { simple } from '@/api/hmmm/subjects.js'
export default {
  name: 'addtags',
  props: ['tablevisible', 'formInline', 'fromSubject'],
  data () {
    return {
      itemlist: {},
      form: {},
      rules: {
        subjectID: [
          { type: 'number', message: '请选择所属学科', tirgger: 'blur' }
        ],
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getlist()
  },
  computed: {
    cTitle () {
      return this.formInline.id ? '修改标签' : '添加标签'
    }
  },
  methods: {
    async getlist () {
      const res = await simple()
      this.itemlist = res.data
    },
    // 关闭弹窗
    gb () {
      this.$emit('close')
      this.formInline.tagName = ''
      this.formInline.subjectID = ''
    },
    // 点击保存提交
    save () {
      // 收集表单信息
      // console.log(this.formInline)
      // 整体表单验证
      this.$refs.formData.validate(async (valid) => {
        if (valid) {
          // 有id则更新调用update()
          if (this.formInline.id) {
            // 发送修改请求
            await update(this.formInline)
            // 修改成功后要把值传给父组件
            this.$emit('getNew', this.formInline)
            this.$message.success('修改成功')
            // 关闭弹窗
            this.gb()
          } else {
            // 发送添加标签请求
            await add(this.formInline)
            this.$emit('getNew', this.formInline)
            this.$message.success('添加成功')
            // 关闭弹窗
            this.gb()
          }
          // 发送请求
          /* const data = {
            subjectID: this.formInline.subjectID,
            tagName: this.formInline.tagName
          } */
          // this.form = this.formInline
        } else {
          this.$message.warning('请输入标签名称或选择学科')
        }
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
